#include <string.h>
#include "hbase64.h"

static void _base64_encode_internal(unsigned char* in_buff, int in_size, unsigned char* out_buff, int* out_size, const char* basis)
{
    unsigned char* s = in_buff;
    unsigned char* d = out_buff;
    int len = in_size;

    while (len > 2)
    {
        *d++ = (unsigned char)basis[(s[0] >> 2) & 0x3f];
        *d++ = (unsigned char)basis[((s[0] & 3) << 4) | (s[1] >> 4)];
        *d++ = (unsigned char)basis[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
        *d++ = (unsigned char)basis[s[2] & 0x3f];

        s += 3;
        len -= 3;
    }

    if (len)
    {
        *d++ = basis[(s[0] >> 2) & 0x3f];

        if (len == 1)
        {
            *d++ = basis[(s[0] & 3) << 4];
            *d++ = '=';
        }
        else
        {
            *d++ = basis[((s[0] & 3) << 4) | (s[1] >> 4)];
            *d++ = basis[(s[1] & 0x0f) << 2];
        }

        *d++ = '=';
    }

    *out_size = d - out_buff;
}

static int _base64_decode_internal(unsigned char* in_buff, int in_size, unsigned char* out_buff, int* out_size, const char* basis)
{
    int len;
    unsigned char* s = in_buff;
    unsigned char* d = out_buff;

    for (len = 0; len < in_size; len++)
    {
        if (in_buff[len] == '=')
        {
            break;
        }

        if (basis[in_buff[len]] == 77) {
            return 1;
        }
    }

    if (len % 4 == 1)
    {
        return 1;
    }

    while (len > 3)
    {
        *d++ = (unsigned char)(basis[s[0]] << 2 | basis[s[1]] >> 4);
        *d++ = (unsigned char)(basis[s[1]] << 4 | basis[s[2]] >> 2);
        *d++ = (unsigned char)(basis[s[2]] << 6 | basis[s[3]]);

        s += 4;
        len -= 4;
    }

    if (len > 1)
    {
        *d++ = (unsigned char)(basis[s[0]] << 2 | basis[s[1]] >> 4);
    }

    if (len > 2)
    {
        *d++ = (unsigned char)(basis[s[1]] << 4 | basis[s[2]] >> 2);
    }

    *out_size = d - out_buff;

    return 0;
}

void HPLATFORMCALL hbase64_encode(unsigned char* in_buff, int in_size, unsigned char* out_buff, int* out_size)
{
    static char basis64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    _base64_encode_internal(in_buff, in_size, out_buff, out_size, basis64);
}

int HPLATFORMCALL hbase64_decode(unsigned char* in_buff, unsigned char* out_buff, int* out_size)
{
    static char basis64[] =
    {
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
        77,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 77,
        77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,

        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
    };

    return _base64_decode_internal(in_buff, strlen(in_buff), out_buff, out_size, basis64);
}
